package cn.antblog.leetcode.link;

/**
 * @author yangjy
 * @version 1.0
 * @description: 移除一个节点
 * @date 2022/6/23 15:24
 */
public class RemoveNode {


    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        //// 因为删除可能涉及到头节点，所以设置dummy节点，统一操作
        ListNode dummy = new ListNode(-1, head);
        ListNode pre = dummy;
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return dummy.next;
    }
    

}
